package com.wondersgroup.fjzlpt.console.service.impl.yljgxx;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.quick.framework.util.ExcelOrWordUtils;
import com.quick.framework.util.FormatDate;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgxx.YljgFrxxDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgxx.YljgRyxxDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgxx.YljgXgxxDAO;
import com.wondersgroup.fjzlpt.console.utils.FileUtils;
import com.wondersgroup.fjzlpt.console.utils.PingYinUtils;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.remoteResult.ResultObject;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgxx.YljgxxDAO;
import com.wondersgroup.fjzlpt.console.service.inter.yljgxx.YljgxxService;

import org.springframework.web.multipart.MultipartFile;

@Service
public class YljgxxServiceImpl implements YljgxxService{

	@Autowired
	private YljgxxDAO yljgxxDao;
	@Autowired
	private YljgFrxxDAO yljgFrxxDAO;
	@Autowired
	private YljgRyxxDAO yljgRyxxDAO;
	@Autowired
	private YljgXgxxDAO yljgXgxxDAO;
	@Autowired
	private FileUtils fileUtils;

	/**
	 * 分页查询
	 * @param map
	 * @return
	 */
	@Cacheable("queryPageYljgxx")
	public JSONObject queryPage(Map<String, String> map)
	{
		return this.yljgxxDao.queryPage(map);
	}

	/**
	 * 分页查询概要信息
	 * @param map
	 * @return
	 */
	@Cacheable("queryPageYljgxx")
	public JSONObject queryPageSummary(Map<String, String> map){
		return this.yljgxxDao.queryPageSummary(map);
	}

	/**
	 * 获取机构图片
	 * @param id
	 * @return
	 */
	public byte[] receiveImage(String id){
		try{
			return fileUtils.receiveImage("JGTP", id);
		}
		catch (Exception e){
			e.printStackTrace();
			throw new RuntimeException("获取机构图片失败", e);
		}
	}

	/**
	 * 保存
	 * @param map
	 * @return
	 */
	@CacheEvict(value={"queryPageYljgxx"}, allEntries=true)
	public JSONObject save(Map map)
	{
		ResultObject ro = new ResultObject();
		String PTJGID = (String)map.get("PTJGID");
		/*处理成立日期和撤销日期*/
		String value = (String)map.get("DWCLRQ");
		if(StringUtils.isEmpty(value)){
			map.put("DWCLRQ", null);
		}else{
			map.put("DWCLRQ", FormatDate.getDateY_M_D(value));
		}
		value = (String)map.get("DWCXRQ");
		if(StringUtils.isEmpty(value)){
			map.put("DWCXRQ", null);
		}else{
			map.put("DWCXRQ", FormatDate.getDateY_M_D(value));
		}
		/**生成机构名称的全拼和简拼*/
		value = (String)map.get("YLJGMC");
		String YLJGMCQP = PingYinUtils.GetFullPinyin(value).toUpperCase();
		map.put("YLJGMCQP", YLJGMCQP);
		String YLJGMCJP = PingYinUtils.GetFirstPinyin(value).toUpperCase();
		map.put("YLJGMCJP", YLJGMCJP);

		try{
			MultipartFile JGTP = (MultipartFile)map.remove("JGTP");
			if(JGTP != null){
				String TPID = fileUtils.saveImageToMongoAndLocal(JGTP, "JGTP", new JSONObject(0));
				map.put("TPID", TPID);
			}
			if(StringUtils.isEmpty(PTJGID))
			{
				insert(map, ro);
			}
			else
			{
				update(map, ro);
			}
		}
		catch (Exception e){
			e.printStackTrace();
			ro.changeFaultState("保存机构信息失败");
			throw new RuntimeException("保存机构信息失败", e);
		}
		return (JSONObject)JSON.toJSON(ro);
	}

	/**
	 * 删除
	 * @param map
	 * @return
	 */
	@CacheEvict(value={"queryPageYljgxx"}, allEntries=true)
	public JSONObject delete(Map<String, String> map)
	{
		ResultObject ro = new ResultObject();
		this.yljgxxDao.deleteForPrimaryKey(map);
		this.yljgRyxxDAO.deleteForPrimaryKey(map);
		this.yljgFrxxDAO.deleteForPrimaryKey(map);
		this.yljgXgxxDAO.deleteForPrimaryKey(map);
		return (JSONObject)JSON.toJSON(ro);
	}
	
	private void insert(Map<String, String> map, ResultObject ro)
	{
		try
		{
			if(this.yljgxxDao.isExist(map))
			{
				ro.changeFaultState("医疗机构代码已存在");
			}
			else
			{
				String PTJGID = UUID.randomUUID().toString();
				map.put("PTJGID", PTJGID);
				ro.setInfo("PTJGID", PTJGID);
				this.yljgxxDao.insert(map);
				this.yljgFrxxDAO.insert(map);
				this.yljgRyxxDAO.insert(map);
				this.yljgXgxxDAO.insert(map);
			}
		}
		catch(Exception e)
		{
			ro.changeFaultState("新增机构失败");
			throw new RuntimeException("新增机构失败", e);
		}
	}
	
	private void update(Map map, ResultObject ro)
	{
		try
		{
			this.yljgxxDao.updateForPrimaryKey(map);
			this.yljgFrxxDAO.deleteForPrimaryKey(map);
			this.yljgFrxxDAO.insert(map);
			this.yljgRyxxDAO.deleteForPrimaryKey(map);
			this.yljgRyxxDAO.insert(map);
			this.yljgXgxxDAO.deleteForPrimaryKey(map);
			this.yljgXgxxDAO.insert(map);
		}
		catch(Exception e)
		{
			ro.changeFaultState("修改机构信息失败");
			throw new RuntimeException("修改机构信息失败", e);
		}
	}


	/**
	 * 将医疗机构信息导出到Excel
	 * @param map
	 * @param request
	 * @param response
	 */
	@Override
	public void downloadToExcel(Map map, HttpServletRequest request, HttpServletResponse response) {
		JSONArray dataArr = yljgxxDao.queryAll(map);
		//表头
		String titleStr = "{'XZQHMC':'行政区划','YLJGDM':'医疗机构代码','YLJGMC':'医疗机构名称','JGJC':'机构简称','WSJGLBMC':'机构类别','YYDJMC':'机构等级'}";
		JSONObject title = JSONObject.parseObject(titleStr);
		dataArr.add(0, title);
		
		//排列循序
		List<String> titleField = new ArrayList<>();
		titleField.add("XZQHMC");
		titleField.add("YLJGDM");
		titleField.add("YLJGMC");
		titleField.add("JGJC");
		titleField.add("WSJGLBMC");
		titleField.add("YYDJMC");

		ExcelOrWordUtils.downLoadExcelFile(dataArr, titleField, "医疗机构列表", request, response);
	}
}




















